<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * BackendPro
 *
 * A website backend system for developers for PHP 4.3.2 or newer
 *
 * @package         conlabz CMS
 * @author          Christian Schneider
 * @copyright       Copyright (c) 2009
 * @license         
 * @link            http://www.conlabz.de
 * @filesource
 */

// ---------------------------------------------------------------------------

/**
 * Website_model
 *
 * Model used to retrive webites
 *
 * @package			conlabz CMS
 * @subpackage		Models
 */
class Website_model extends Base_model {
	function Website_model( ) {
		parent::Base_model( );

		$this->_TABLES = array( 'Option' => $this->config->item( 'backendpro_table_prefix' ) . 'article' );

		// Cache to store already fetched items
		$this->_CACHE = array( );

		// Object keyword
		// I wouldn't advise changing this, it could corrupt current
		// articles, the reason for needing this is to stop unserialze
		// errors spamming the log files
		$this->object_keyword = "BeP::Object::";

		log_message( 'debug', 'conlabz CMS : Website_model class loaded' );
	}

	/**
	 * Get Website
	 *
	 * @access public
	 * @param mixed $where Where query string/array
	 * @param array $limit Limit array including offset and limit values
	 * @return object
	 */
	function getWebsites( $where = NULL, $limit = array( 'limit' => NULL, 'offset' => '' ) ) {
		$this->db->select( 'website.id,
							website.master_language_id,
							website.classification_id,
							website.template,
							website.name,
							website.active'
						   );
		$this->db->from( "website" );
		if( !is_null( $where ) ){
			$this->db->where( $where );
		}
		if( !is_null( $limit['limit'] ) ){
			$this->db->limit( $limit['limit'], ( isset( $limit['offset'] ) ? $limit['offset'] : '' ) );
		}
		$this->db->order_by('website.name desc'); 
		return $this->db->get( );
	}
	
	/**
	 * Constructs an array of the given websites to be used for a dropdown menu
	 *
	 * @access public
	 * @return array
	 */
	function buildWebsiteDropdown( ) {
		$websites = $this->getWebsites( );
		
		$dropdown = array( );
		foreach( $websites->result( ) as $row ) {
	    	 $dropdown[$row->id] = $row->name;
		}
		
		return $dropdown;
	}
	
	/**
	 * Insert new Website
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function insert( $data = array( ) ) {
		//create new website entry
		$this->db->set( 'id',                 $data['id']                 );
		$this->db->set( 'name',               $data['name']               );
		$this->db->set( 'active',             $data['active']             );
		$this->db->set( 'template',           $data['template']           );
		$this->db->set( 'master_language_id', $data['language']           );
		$this->db->set( 'classification_id',  $data['classification_id']  );
		//insert the new website
		$this->db->insert( 'website' );
		
		$website_id = $this->db->insert_id( );
		
		//insert default category
		$this->db->set( 'language_id',		$data['language']           );
		$this->db->set( 'website_id',		$website_id		            );
		$this->db->set( 'name',				"Standard"           		);
		$this->db->set( 'description',		"Standardkategorie"    		);
		$this->db->set( 'lft',				'1'			           		);
		$this->db->set( 'rgt',				'2'           				);
		$this->db->set( 'url_title',		"standard"           		);
		//insert the default category
		$this->db->insert( 'category' );
		
		//insert one region (body)
		$this->db->set( 'website_id',		$website_id		            );
		$this->db->set( 'region',			"body"        		   		);
		//insert into table
		$this->db->insert( 'region' );
		
		//insert corporate information
		$this->db->set( 'website_id',		$website_id		            );
		$this->db->set( 'company',          $data['name']               );
		//insert into table
		$this->db->insert( 'corporate' );
		
		//insert new value into for user <-> website
		$this->db->set( 'user_id',             $data['user_id']          );
		$this->db->set( 'website_id',          $website_id               );
		
		$this->db->insert( 'user_website' );
		
		//make template directory
		$path = './';
		//template
		mkdir( $path . 'template/' . $data['template'], 0755 );
		//views
		mkdir( $path . 'modules/welcome/views/template/' . $data['template'], 0755 );
		//upload
		mkdir( $path . 'uploads/' . $data['template'], 0777 );
		
		return $website_id;
	}
	
	/**
	 * Update existing Website
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function update( $data = array( ), $id ) {		
		//create new website entry
		$website = array( );
		//need to rename field from post to database
		$website['name'] 				= 	$data['name'];
		$website['active'] 				= 	$data['active'];
		$website['template'] 			= 	$data['template'];
		$website['master_language_id'] 	= 	$data['language'];
		$website['classification_id'] 	= 	$data['classification_id'];
		
		//insert the new website
		$this->db->update( 'website', $website, array( 'id' => $id ) );
		
		//delete entry form user <-> website
		$this->db->delete( 'user_website', array( 'website_id' => $id ) );
		
		//insert new value into for user <-> website
		$this->db->set( 'user_id',             $data['user_id']  );
		$this->db->set( 'website_id',          $id               );
		
		$this->db->insert( 'user_website' );
	}
	
	/**
	 * Delete existing Website
	 *
	 * @access public
	 * @param int $id Id of the website
	 */
	function delete( $id ){
		//delete the website
		$this->db->delete( 'website', array( 'id' => $id ) );
		
		//delete entry form user <-> website
		$this->db->delete( 'user_website', array( 'website_id' => $id ) );
	}
	
	/**
	 * Get Website information by given user id
	 *
	 * @access public
	 * @param int $id Id of the user
	 */
	function getWebsiteInfosByUserId( $id, $admin = false ){
		if( $admin ){
			$this->db->select( 'website.id AS website_id, website.template' );
			$this->db->from( "website" );
			$this->db->where( "`website`.`id`" , $id );
		} else {
			$this->db->select( 'user_website.website_id, 
								website.template'
							   );
			$this->db->from( "user_website, website" );
			$this->db->where( "`user_website`.`user_id`" , $id );
			$this->db->where( "`user_website`.`website_id`=`website`.`id`");
		}
		$query = $this->db->get( );
		$row = $query->result_array( );
		if( !empty( $row ) ) {
			return $row[0];
		}
	}
	
	/**
	 * Get Website by Domain
	 *
	 * @access public
	 * @param mixed $where Where query string/array
	 * @param array $limit Limit array including offset and limit values
	 * @return object
	 */
	function getWebsiteByDomain( $domain ) {
		$this->db->select( 'website.id,
							website.master_language_id,
							website.classification_id,
							website.template,
							website.name,
							website.active'
						   );
		$this->db->from( "website" );
		$this->db->from( "domain" );
		
		$this->db->where( "website.id = domain.website_id AND domain.url = '" . $domain . "'" );
		
		$website = $this->db->get( );
		
		$row = $website->result_array( );
		
		return $row[0];
	}
	
	/**
	 * Get Website User Menu
	 *
	 * @access public
	 * @return object
	 */
	function getWebsiteUserMenu( ) {
		$pages = array( );
		if( check( 'Category', NULL, FALSE ) ){
			$pages['category'] = array( 'url' => 'article/user/category', 'name' => 'Kategorien', 'icon' => 'icon_group' );
		}
		if( check( 'Article', NULL, FALSE ) ){
			$pages['article'] = array( 'url' => 'article/user/article', 'name' => 'Artikel', 'icon' => 'icon_group' );
		}
		if( check( 'Menu', NULL, FALSE ) ){
			$pages['menu'] = array( 'url' => 'article/user/menu', 'name' => 'Men&uuml;', 'icon' => 'icon_group' );
		}
		
		$modules = $this->getComponentByFeature( );

		$return = array( 'home' => array( 'url' => '', 'name' => 'Dashboard', 'icon' => 'icon_house' ), 
					  'pages' => array( 'url' => 'article/user/pages', 'name' => 'Seiten', 'icon' => 'icon_computer', 
					  		'sub' => $pages ),
					  'files' => array( 'url' => 'article/user/upload', 'name' => 'Dateien', 'icon' => 'icon_house' ),
					  'mgt' => array( 'url' => 'article/user/management', 'name' => 'Management', 'icon' => 'icon_computer', 
					  		'sub' => $modules ),
					  'imprint' => array( 'url' => 'article/user/corporate', 'name' => 'Impressum', 'icon' => 'icon_house' ),
					  'trash' => array( 'url' => 'article/user/trash', 'name' => 'Papierkorb', 'icon' => 'icon_house' ),
					  'help' => array( 'url' => 'article/user/help', 'name' => 'Hilfe', 'icon' => 'icon_house' ),
					);
		return $return;
	}
	
	function getComponentByFeature( ){
		$this->db->select( 'components.name,
							components.module,
							components.controller,
							components.form,
							components.icon'
						   );
		$this->db->from( "components" );
		$this->db->from( "feature_set" );
		
		$this->db->where( "feature_set.component_id = components.id AND feature_set.website_id = '" . $this->session->userdata( 'website_id' ) . "'" );
		
		$website = $this->db->get( );
		
		$row = $website->result_array( );
		$modules = array( );
		$i = 0;
		foreach( $website->result_array( ) as $row ){
			$modules[$i]['url'] = $row['module']."/user/".$row['controller'];
			$modules[$i]['module'] = $row['module'];
			$modules[$i]['controller'] = $row['controller'];
			$modules[$i]['name'] = $row['name'];
			$modules[$i]['icon'] = $row['icon'];
			$modules[$i]['form'] = $row['form'];
			$i++;   
		}
		
		return $modules;
	}
}
/* End of file website_model.php */
/* Location: ./modules/articles/models/website_model.php */
